package yourauthor.utils;

import java.util.Comparator;
import java.util.Map;

/**
 *
 * @author ross
 */
public class WordMapComparator implements Comparator {
    
    Map<String, Integer> originalMap;
    
    public WordMapComparator(Map originalMap) {
        this.originalMap = originalMap;
    }

    @Override
    public int compare(Object t, Object t1) {
        
        if ( originalMap.get(t) > originalMap.get(t1) ) //Compare descending order if the values have are different
            return -1;
        if ( originalMap.get(t) == originalMap.get(t1) ) { //If the values are the same, compare the keys
            if ( t instanceof String && t1 instanceof String ) {
                if ( ((String)t).compareTo((String)t1) > 0 ) {
                    return 1;
                } else if ( t.equals(t1) ) {
                    return 0;
                } else
                    return -1;
            }
            return 0;
        }
        return 1;
    }    
    
}
